package com.emc.elc.rabbitmq.wapper.Handler;

import com.emc.elc.rabbitmq.wapper.Handler.excutor.HandlerExcutorService;
import com.rabbitmq.client.AMQP.BasicProperties;

public abstract class AsynMessageHandler implements MessageHandler {
    @Override
    public void handle(String msg, String routingKey, BasicProperties basicProperties) {
        HandleTask task = new HandleTask(getMessageHandler(msg, routingKey), msg, routingKey, basicProperties);
        HandlerExcutorService.getExcutor().execute(task);
    }
    
    protected abstract void bindMessageHandler (String bindingKey, MessageHandler messageHandler);
    protected abstract MessageHandler getMessageHandler (String msg, String routingKey);

    private class HandleTask implements Runnable {
        MessageHandler handler;
        String msg;
        String routingKey;
        BasicProperties basicProperties;

        public HandleTask(MessageHandler handler, String msg, String routingKey, BasicProperties basicProperties) {
            this.handler = handler;
            this.msg = msg;
            this.routingKey = routingKey;
            this.basicProperties = basicProperties;
        }

        @Override
        public void run() {
            handler.handle(msg, routingKey, basicProperties);
        }

    }
}
